package com.tanhua.recommend.listeners;

import com.alibaba.fastjson.JSON;
import com.tanhua.domain.mongo.Movement;
import com.tanhua.domain.mongo.PublishScore;
import com.tanhua.dubbo.api.mongo.MovementApi;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Component
public class MovementScoreListener {

    @DubboReference
    private MovementApi movementApi;

//      rabbitTemplate.convertAndSend("movement.score.exchange","movement.score",jsonString);
//      map.put("userId",UserHolder.getUserId());
//        map.put("movementId",movementId);
//        map.put("type",type);

    @Autowired
    private MongoTemplate mongoTemplate;

    @RabbitListener(bindings=@QueueBinding(value = @Queue(name="movement.score.queue"),exchange = @Exchange(name = "movement.score.exchange",
            type = ExchangeTypes.DIRECT),key = "movement.score"))
    public void movementCheck(String jsonString) throws Exception{
        Map map = JSON.parseObject(jsonString, Map.class);
        long userId = Long.parseLong(map.get("userId").toString());
        String movementId = (String) map.get("movementId");
        Integer type = (Integer) map.get("type");

        Movement movement = mongoTemplate.findById(new ObjectId(movementId), Movement.class);

        PublishScore publishScore = new PublishScore();
        publishScore.setId(new ObjectId());
        publishScore.setUserId(userId);
        publishScore.setPublishId(movement.getPid());
        publishScore.setDate(new Date().getTime());
        Double score = 0.0;
        switch (type){
            case 1:{
                score = 1.0;
                break;
            }
            case 2:{
                score = 5.0;
                break;
            }
            case 3:{
                score = 8.0;
                break;
            }
            case 4:{
                score = 10.0;
                break;
            }
            case 5:{
                score = -5.0;
                break;
            }
            case 6:{
                score = -8.0;
                break;
            }
            default:{
                break;
            }
        }
        publishScore.setScore(score);

        mongoTemplate.save(publishScore);

        System.out.println("执行了MovementScoreListener中的方法。。。。。。。。。。。。。。。。。。。。。");


    }



}
